﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace comm
{
    public partial class datatest : Form
    {
        BLL.tbl_Sensor bsen = new BLL.tbl_Sensor();
        private string lk = "";
        private Thread theead;
        public datatest()
        {
            InitializeComponent();
            //Thread thread = new Thread(new ThreadStart(DoWork));
            theead = new Thread(new ThreadStart(DoWork));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable mmm = bsen.GetList("1=1").Tables[0];
            //theead = new Thread(new ThreadStart(DoWork));

            if (theead.ThreadState == ThreadState.Suspended)
            {
                theead.Resume();
            }
            else
            {
                theead.Start();
            }
            this.label1.Text = theead.ThreadState.ToString();
        }

        private void DoWork()
        {

            DataTable mmm = bsen.GetList("1=1").Tables[0];

            for (int i = 0; i < mmm.Rows.Count; i++)
            {
                string dz = mmm.Rows[i]["SensorCode"].ToString() + Environment.NewLine + Environment.NewLine;

                if (i == mmm.Rows.Count - 1)
                {
                    i = -1;
                }
                if (lk == "1")
                {
                    break;
                }
                else
                {
                    Thread.Sleep(2000);
                    this.Invoke(new Action<string>(this.ChangetextBox), dz);
                }
            }
        }

        private void ChangetextBox(string zf)
        {
            this.textBox1.AppendText(zf);
            this.textBox1.ScrollToCaret();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (theead.ThreadState == ThreadState.Running)
                {
                    lk = "1";
                    //theead.Abort();
                    theead.Suspend();
                }
            }
            catch (ThreadStateException ex)
            {
                MessageBox.Show(ex.ToString(), "Exception",MessageBoxButtons.OK, MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (theead.ThreadState == ThreadState.SuspendRequested || theead.ThreadState == ThreadState.Suspended)
            {
                try
                {
                    theead.Resume();
                }
                catch (ThreadStateException ex)
                {
                    MessageBox.Show(ex.ToString(), "Exception",MessageBoxButtons.OK, MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
                }
            }
        }
    }
}